<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import logger from '@/utils/logger'
import { fetchMyOnlineGoods } from '@/api/goodsRemovalApi'
import GoodsCommentList from './components/GoodsCommentList.vue'

// 表格数据和加载状态
const tableData = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 查询条件
const queryFields = ref({
  goodsName: '',
  goodsTypeName: '',
})

// 评论弹窗控制
const commentDialogVisible = ref(false)
const currentRow = ref(null)

const openCommentDialog = (row) => {
  currentRow.value = row
  commentDialogVisible.value = true
}
const closeCommentDialog = () => {
  commentDialogVisible.value = false
  currentRow.value = null
}

// 获取库存商品列表
const fetchStoreData = async () => {
  loading.value = true
  try {
    const params = {
      queryFields: {
        goodsName: queryFields.value.goodsName,
        goodsTypeName: queryFields.value.goodsTypeName,
      },
      currentPage: currentPage.value,
      pageSize: pageSize.value,
    }
    const res = await fetchMyOnlineGoods(params)

    if (res.success) {
      tableData.value = res.data.list || []
      total.value = res.data.total || 0
    } else {
      ElMessage.error('获取库存商品列表失败')
    }
  } catch (error) {
    logger.error('获取库存商品列表失败', error)
    ElMessage.error('获取库存商品列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchStoreData()
}

// 重置搜索
const resetSearch = () => {
  queryFields.value = {
    goodsName: '',
    goodsTypeName: '',
  }
  currentPage.value = 1
  fetchStoreData()
}

// 分页处理
const handleSizeChange = (size) => {
  pageSize.value = size
  fetchStoreData()
}

const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchStoreData()
}

onMounted(() => {
  fetchStoreData()
})
</script>

<template>
  <div class="main-container">
    <el-dialog
      v-model="commentDialogVisible"
      title="商品评论"
      width="80%"
      align-center
      append-to-body
      destroy-on-close
      @close="closeCommentDialog"
    >
      <GoodsCommentList
        :dealer-goods-store-id="currentRow?.dealerGoodsStoreId"
        v-if="commentDialogVisible"
      />
    </el-dialog>
    <div class="controller-container">
      <el-form :inline="true" :model="queryFields">
        <el-form-item label="商品名称">
          <el-input
            v-model="queryFields.goodsName"
            placeholder="请输入商品名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleSearch"
          >
            <template #prefix>
              <el-icon><LineMdSearch /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="商品类别">
          <el-input
            v-model="queryFields.goodsTypeName"
            placeholder="请输入商品类别"
            clearable
            style="width: 200px"
            @keyup.enter="handleSearch"
          >
            <template #prefix>
              <el-icon><LineMdSearch /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <template #icon>
              <el-icon><LineMdSearch /></el-icon>
            </template>
            搜索
          </el-button>
          <el-button @click="resetSearch">
            <template #icon>
              <el-icon><LineMdClose /></el-icon>
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="tableData && tableData.length > 0 ? tableData : [{}]"
        style="width: 100%"
      >
        <el-table-column label="图片" width="80" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              <el-image
                v-if="row.goodsPicture"
                :src="row.goodsPicture"
                style="width: 50px; height: 50px; border-radius: 4px"
                :preview-src-list="[row.goodsPicture]"
                fit="cover"
                preview-teleported
              />
              <div v-else class="no-image">无图片</div>
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column
          prop="goodsName"
          label="商品名称"
          min-width="150"
          show-overflow-tooltip
          align="center"
        >
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.goodsName || '-' }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="dealerGoodsTypeName" label="商品类别" min-width="120" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.dealerGoodsTypeName || '-' }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="xzqhName" label="专属区域" min-width="120" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.xzqhName || '-' }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="specifications" label="规格" min-width="120" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.specifications || '-' }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="库存数量" min-width="120" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              <el-tag type="success"> {{ row.quantity || '-' }}</el-tag>
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="unitPrice" label="单价(元)" min-width="120" align="center" />
        <el-table-column prop="shippingFee" label="配送费(元)" min-width="120" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.shippingFee || '0.00' }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="freeShipping" label="免配送费数量" min-width="130" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.freeShipping || 0 }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="unit" label="单位" min-width="80" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.unit || '-' }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="产地" min-width="120" align="center">
          <template #default="{ row }">
            <template v-if="tableData && tableData.length > 0">
              {{ row.address || '-' }}
            </template>
            <template v-else> - </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="openCommentDialog(row)">
              <template #icon>
                <el-icon><LineMdChatRoundDots /></el-icon>
              </template>
              查看评论
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.main-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  gap: 10px;
  .controller-container {
    border-radius: 10px;
    background-color: #fff;
    .el-form {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 10px;
    }
  }
  .table-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background-color: #fff;
    border-radius: 10px;
    padding: 15px;
    overflow: hidden;
    gap: 10px;
    .el-table {
      flex: 1;
    }
    .no-image {
      width: 50px;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #909399;
      background-color: #f5f7fa;
      font-size: 12px;
      border-radius: 4px;
    }
  }
  .pagination-container {
    display: flex;
    justify-content: flex-end;
    align-items: flex-end;
  }
}
</style>
